﻿Public Class OptionsViewModel
  Inherits BaseViewModel

  Private _showCommand As RelayCommand
  Private _okCommand As RelayCommand
  Private _cancelCommand As RelayCommand
  Private _obView As OptionsView
  Private _result As Boolean


  Private _TFSServerSelectedCommand As RelayCommand
  Private _TFSProjectSelectedCommand As RelayCommand

#Region "View Properties"

  Public ReadOnly Property TFSServers() As List(Of String)
    Get
      Return Global.SfTSQuickBoard.TFSServer.Servers
    End Get
  End Property

  Public Property TFSServer() As String
    Get
      Return My.Settings.TFSServerName
    End Get
    Set(ByVal value As String)
      If My.Settings.TFSServerName <> value Then
        My.Settings.TFSServerName = value
        OnPropertyChanged("TFSServer")
      End If

    End Set
  End Property

  Public ReadOnly Property OptionsChanged() As Boolean
    Get
      Return _result
    End Get
  End Property

  Public ReadOnly Property TFSProjects() As List(Of String)
    Get
      Return Global.SfTSQuickBoard.TFSServer.Projects(Me.TFSServer)
    End Get
  End Property

  Public Property TFSProject() As String
    Get
      Return My.Settings.TeamProjectName
    End Get
    Set(ByVal value As String)
      If My.Settings.TeamProjectName <> value Then
        My.Settings.TeamProjectName = value
        OnPropertyChanged("TFSProject")
      End If

    End Set
  End Property

  Public ReadOnly Property TFSIterations() As List(Of String)
    Get
      Return Global.SfTSQuickBoard.TFSProject.Iterations(TFSServer, TFSProject).Keys.ToList
    End Get
  End Property

  Public Property TFSIteration() As String
    Get
      Return My.Settings.IterationPath
    End Get
    Set(ByVal value As String)
      If My.Settings.IterationPath <> value Then
        My.Settings.IterationPath = value
        OnPropertyChanged("TFSIteration")
      End If

    End Set
  End Property

  Public Property AutoRefresh() As Boolean
    Get
      Return My.Settings.AutoRefresh
    End Get
    Set(ByVal value As Boolean)
      If My.Settings.AutoRefresh <> value Then
        My.Settings.AutoRefresh = value
        OnPropertyChanged("AutoRefresh")
      End If
    End Set
  End Property

  Public Property RefreshIntervalSeconds() As Integer
    Get
      Return My.Settings.RefreshIntervalSeconds \ 60
    End Get
    Set(ByVal value As Integer)
      My.Settings.RefreshIntervalSeconds = CInt(value) * 60
      OnPropertyChanged("RefreshIntervalSeconds")
    End Set
  End Property

#End Region

#Region "Commands"

  Public ReadOnly Property ShowCommand() As ICommand
    Get
      If _showCommand Is Nothing Then
        _showCommand = New RelayCommand(New Action(Of Object)(AddressOf OnShowCommand))
      End If

      Return _showCommand
    End Get
  End Property

  Public ReadOnly Property OkCommand() As ICommand
    Get
      If _okCommand Is Nothing Then
        _okCommand = New RelayCommand(New Action(Of Object)(AddressOf OnOkCommand))
      End If

      Return _okCommand
    End Get
  End Property

  Public ReadOnly Property CancelCommand() As ICommand
    Get
      If _cancelCommand Is Nothing Then
        _cancelCommand = New RelayCommand(New Action(Of Object)(AddressOf OnCancelCommand))
      End If

      Return _cancelCommand
    End Get
  End Property

  Public ReadOnly Property TFSServerSelectedCommand() As ICommand
    Get
      If _TFSServerSelectedCommand Is Nothing Then
        _TFSServerSelectedCommand = New RelayCommand(New Action(Of Object)(AddressOf OnTFSServerSelected))
      End If

      Return _TFSServerSelectedCommand
    End Get
  End Property

  Public ReadOnly Property TFSProjectSelectedCommand() As ICommand
    Get
      If _TFSProjectSelectedCommand Is Nothing Then
        _TFSProjectSelectedCommand = New RelayCommand(New Action(Of Object)(AddressOf OnTFSProjectSelected))
      End If

      Return _TFSProjectSelectedCommand
    End Get
  End Property

#End Region

#Region "Command Handlers"

  Private Sub OnOkCommand(ByVal e As Object)
    _obView.DialogResult = True
    _result = True
    _obView.Close()
    My.Settings.Save()
  End Sub

  Private Sub OnCancelCommand(ByVal e As Object)
    _obView.DialogResult = False
    _result = False
    _obView.Close()

  End Sub

  Private Sub OnShowCommand(ByVal e As Object)
    _obView = New OptionsView
    _obView.DataContext = Me

    _obView.ShowDialog()

  End Sub

  Private Sub OnTFSServerSelected(ByVal e As Object)
    OnPropertyChanged("TFSProjects")
  End Sub

  Private Sub OnTFSProjectSelected(ByVal e As Object)
    OnPropertyChanged("TFSIterations")
  End Sub
#End Region

End Class
